package com.david.avcodec.system

import android.app.Activity
import android.media.MediaPlayer
import android.media.MediaRecorder
import android.os.Build
import android.os.Bundle
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.widget.VideoView
import com.david.avcodec.R


//mediaRecorder录制视频、meidiaPlayer播放视频
//参考链接：https://developer.aliyun.com/article/587677   https://blog.csdn.net/oHeHui1/article/details/131138460  https://www.cnblogs.com/guanxinjing/p/10980906.html
class MyVideoMediaRecorderActivity: Activity() {

    private lateinit var surfaceView: SurfaceView
    private lateinit var record: MediaRecorder

    private lateinit var vv: VideoView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_video)

        surfaceView = findViewById(R.id.surface)
        surfaceView.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.holder.setKeepScreenOn(true);


        record = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            MediaRecorder(this)
        } else {
            MediaRecorder()
        }

        record.setAudioSource(MediaRecorder.AudioSource.MIC)
        record.setVideoSource(MediaRecorder.VideoSource.CAMERA)
        record.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        record.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT)
        record.setVideoEncoder(MediaRecorder.VideoEncoder.HEVC)
        record.setOutputFile("${externalCacheDir}/test.mp4")


        record.setPreviewDisplay(surfaceView.holder.surface)


        findViewById<View>(R.id.start).setOnClickListener {
            record.prepare()
            record.start()
        }

        findViewById<View>(R.id.stop).setOnClickListener {
            record.stop()
            record.release()
        }


        findViewById<View>(R.id.start_play).setOnClickListener {
            vv.start()
        }

        findViewById<View>(R.id.stop_play).setOnClickListener {
            vv.stopPlayback()
        }


        vv = findViewById<View>(R.id.video) as VideoView
        vv.setVideoPath("${externalCacheDir}/test.mp4")
//        vv.setOnPreparedListener { vv.start() }
        //设置video的控制器
        //设置video的控制器
//        vv.setMediaController(MediaController(this))
    }
}